package cp2013;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author AcerXPS
 */
public class UserPanel extends JPanel {

    private JLabel lblUsername;
    private JLabel lblToken;
    private User user;

    public void setUser(User user) {
        this.user = user;
    }

    public void loadPanel() {
        lblUsername = new JLabel("Username: " + user.getUsername());
        lblUsername.setFont(new Font("Impact", 0, 24));

        lblToken = new JLabel("Token: " + insertCommas(String.valueOf(user.getToken())));
        lblToken.setFont(new Font("Impact", 0, 24));

        setLayout(new GridLayout(1, 2));
        setBackground(Color.YELLOW);
        setVisible(true);


        add(lblUsername);
        add(lblToken);
    }//end of loadPanel()

    public void refreshUserPanel() {
        lblUsername.setText("Username: " + user.getUsername());
        lblToken.setText("Token: " + user.getToken());
    }

    public String insertCommas(String str) {
        String newStr = str;
        int firstComma = str.indexOf(",");
        if (firstComma < 0) {
            firstComma = str.length();
        }
        if (str.length() > 3 && firstComma > 3) {
            newStr = insertCommas(new StringBuffer(str).insert(firstComma - 3, ",").toString());
        }
        return newStr;
    }
}
